package com.yc.supermarket.service.impl;

import com.yc.supermarket.entity.*;
import com.yc.supermarket.mapper.GoodsInfoMapper;
import com.yc.supermarket.service.GoodsInfoService;
import com.yc.supermarket.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.util.*;

public class GoodsInfoServiceImpl implements GoodsInfoService {
    private GoodsInfoMapper mapper;

    @Override
    public Map<String, Object> getCommoditiesByTypeId(Integer typeId) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        List<Commodity> list = mapper.getCommoditiesByTypeId(typeId);
        Map<String, Object> m = new HashMap<String, Object>();
        if (null == list || list.isEmpty()) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        return m;
    }

    @Override
    public Map<String, Object> getCommoditiesByComId(Integer commodityId) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        List<Commodity> list = mapper.getCommoditiesByComId(commodityId);
        Map<String, Object> m = new HashMap<String, Object>();
        if (null == list || list.isEmpty()) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        return m;
    }

    @Override
    public Map<String, Object> getCommodities() {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        List<Commodity> list = mapper.getCommodities();
        //System.out.println(mapper.getCommoditiesByTypeId(typeId)+"=====================");
        Map<String, Object> m = new HashMap<String, Object>();
        if (null == list || list.isEmpty()) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        return m;
    }

    @Override
    public Map<String, Object> getCollectById(Integer commodityId) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        List<Commodity> list = mapper.selectCommoditiesByUserId(commodityId);
        Map<String, Object> m = new HashMap<String, Object>();
        if (null == list || list.isEmpty()) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        return m;
    }

    @Override
    public Map<String, Object> getProductType() {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        List<ProductType> list = mapper.getProductType();
        //System.out.println(mapper.getProductType()+"=====================");
        Map<String, Object> m = new HashMap<String, Object>();
        if (null == list || list.isEmpty()) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        //return mapper.getProductType();
        System.out.println(m);
        return m;

    }

    @Override
    public Map<String, Object> getCommodityList() {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        List<Commodity> list = mapper.getCommodityList();

        Map<String, List<Map<String, Object>>> typeMap = new HashMap<>();
        for (Commodity commodity : list) {
            String typeName = commodity.getProductType().getTypeName();
            int typeId = commodity.getProductType().getTypeId();
            if (!typeMap.containsKey(typeName)) {
                typeMap.put(typeName, new ArrayList<>());
            }

            Map<String, Object> commodityData = new HashMap<>();
            commodityData.put("commodityId", commodity.getCommodityId());
            commodityData.put("commodityName", commodity.getCommodityName());

            typeMap.get(typeName).add(commodityData);
            typeMap.get(typeName).get(typeMap.get(typeName).size() - 1).put("typeId", typeId);
        }

        Map<String, Object> response = new HashMap<>();
        if (typeMap.isEmpty()) {
            response.put("code", 600);
            response.put("msg", "暂无数据");
        } else {
            response.put("code", 200);
            response.put("msg", "成功");

            List<Map<String, Object>> formattedData = new ArrayList<>();
            for (Map.Entry<String, List<Map<String, Object>>> entry : typeMap.entrySet()) {
                String typeName = entry.getKey();
                List<Map<String, Object>> commodityList = entry.getValue();

                Map<String, Object> typeData = new HashMap<>();
                typeData.put("typeId", commodityList.get(0).get("typeId"));
                typeData.put("typeName", typeName);
                typeData.put("commodityList", commodityList);

                formattedData.add(typeData);
            }

            response.put("data", formattedData);
        }

        return response;
    }

    @Override
    public Map<String, Object> getUserByName(String userName) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        List<User> list = mapper.getUserByName(userName);
        Map<String, Object> m = new HashMap<String, Object>();
        if (null == list || list.isEmpty()) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        return m;
    }

    @Override
    public int becomeMember(String userName, BigDecimal newBalance, String registrationDate) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        User user = mapper.getUserByUserName(userName);
        if (user != null) {
            BigDecimal userBalance = user.getBalance();
            BigDecimal memberBalance = userBalance != null ? userBalance.add(newBalance) : newBalance;
            mapper.updateUserBalance(user.getUserId(), memberBalance);

            Member member = new Member();
            member.setUserId(user.getUserId());
            member.setBalance(memberBalance);
            member.setRegistrationDate(registrationDate);
            mapper.insertMember(member);
        } else {
            return -1;
        }
        return 1;
    }

    @Override
    public int addCollect(Collect collect) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        int i = mapper.addCollect(collect);
        return i;
    }

    @Override
    public int getUserById(String userName) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        User user = mapper.getUserByUserName(userName);
        if (user != null) {
            int userId = user.getUserId();
            List<Member> member = mapper.getUserById(userId);
            if (member != null && !member.isEmpty()) {
                return 1;
            } else {
                return -1;
            }
        } else {
            return -1;
        }

    }

    @Override
    public Map<String, Object> getMemberInfoById(String userName) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        User user = mapper.getUserByUserName(userName);
        int userId = user.getUserId();
        List<Member> list = mapper.getUserById(userId);
        Map<String, Object> m = new HashMap<String, Object>();
        if (null == list || list.isEmpty()) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        return m;
    }

    @Override
    public int addBalance(String userName, BigDecimal newBalance) {

        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        User user = mapper.getUserByUserName(userName);
        if (user != null) {
            BigDecimal userBalance = user.getBalance();
            BigDecimal memberBalance = userBalance != null ? userBalance.add(newBalance) : newBalance;
            mapper.updateUserBalance(user.getUserId(), memberBalance);

            Member member = new Member();
           /* System.out.println("member.getUserId(): " + member.getUserId());
            System.out.println("user.getUserId(): " + user.getUserId());*/
            List<Member> list = mapper.getUserById(user.getUserId());
            if (null != list && !list.isEmpty()) {
                //System.out.println(Integer.valueOf(member.getUserId()).equals(user.getUserId())+"++++++++++++++++++++");
                BigDecimal balance = userBalance.add(newBalance);
                mapper.updateMemberBalance(user.getUserId(), balance);
            }
        }
            else {
                return -1;
            }
            return 1;
        }

    @Override
    public int delCollect(Integer userId, Integer commodityId) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        int i = mapper.deleteCollect(userId, commodityId);
        return i;
    }
}


